package com.aiit.service.impl;

import com.aiit.dao.SysUserDao;
import com.aiit.pojo.SysUser;
import com.aiit.service.SysUserService;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;


@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    SysUserDao sysUserDao;

    @Override
    public String getPasswordByUserName(String userName) {
        // TODO Auto-generated method stub
        SysUser sysUser  = sysUserDao.getUserByName(userName);
        if(null==sysUser)
            return null;
        return sysUser.getPassword();
    }

    @Override
    public SysUser getUserByUserName(String userName) {
        SysUser sysUser  = sysUserDao.getUserByName(userName);
        if(null==sysUser)
            return null;
        return sysUser;
    }

    /**
     * 添加用户--注册
     * @param sysUser
     */
    @Override
    public void addSysUser(SysUser sysUser) {
                 //随机盐值
                String salt= UUID.randomUUID().toString();
                //密码的密文
                String password=new Sha256Hash(sysUser.getPassword(),salt,10).toBase64();
                sysUser.setPassword(password);
                sysUser.setSalt(salt);
                sysUserDao.insertSysUser(sysUser);
    }

}

